﻿/*
 *  Source code courtesy of the desktopWeb.CodePlex.com community project. See MS-PL license on Codeplex.com. 
 *  This is beta code not intended for a production application.
 */

using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using MyData.ExpressionBuilder;
using MyData.ExpressionBuilder.BaseExtensions;
using System.Threading;
using System.Windows.Forms;
using System;
using MyData.ExpressionBuilder.Model;
using System.Collections.ObjectModel;
using System.Windows.Controls;
using MyData.ExpressionBuilder.Interfaces;

namespace MyData.Extensions
{
  [ExpressionBuilderMetadata(Name = "NameValueFormBuilder", Description = "My Test Builder", ImagePath = "Images/TestBuilder.jpg")]
  public class NameValueFormBuilder : ExpressionBuilderBase
  {
    private ObservableCollection<ListItem> _result = new ObservableCollection<ListItem>();
    Action<ObservableCollection<ListItem>> _onUpdated;

    protected override IEnumerable<ListItem> RunExtension(IEnumerable<ListItem> metadata)
    {
      
      SampleExpressionForm f = new SampleExpressionForm(metadata, this, base.OnUpdated);
      f.ShowDialog(new MyWin32Window(System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle));
   
      return metadata; 
    }

  }
 
  [ExpressionBuilderMetadata(Name = "SimpleBuilder", Description = "My Test Builder", ImagePath = "Images/TestBuilder.jpg")]
  public class SimpleBuilder : ExpressionBuilderBase
  {
    private ObservableCollection<ListItem> _result = new ObservableCollection<ListItem>();
    Action<ObservableCollection<ListItem>> _onUpdated;

    protected override IEnumerable<ListItem> RunExtension(IEnumerable<ListItem> metadata)
    {
      
      SimpleExpressionForm f = new SimpleExpressionForm(metadata, base.OnUpdated);
      f.ShowDialog(new MyWin32Window(System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle));   
      
      return metadata;
    }
  }



  [ExpressionBuilderMetadata(Name = "MenuItemsBuilder", Description = "My Test Builder", ImagePath = "Images/TestBuilder.jpg")]
  public class MenuItemsBuilder : ExpressionBuilderBase
  {
    Action<ObservableCollection<ListItem>> _onUpdated;
    private ObservableCollection<ListItem> _result = new ObservableCollection<ListItem>();

    private System.Windows.Controls.MenuItem _contextMenu = null;

    protected override IEnumerable<ListItem> RunExtension(IEnumerable<ListItem> metadata)
    {
      _contextMenu = base.ItemsControl as System.Windows.Controls.MenuItem;

      if (this.ItemsProvider != null)
      {
        this.ItemsProvider.MetaData = metadata;

        foreach (ListItem item in this.ItemsProvider.Items)
        {
          _contextMenu.Items.Add(AddMenuItem(item.Name, item.Value.ToString()));
        }
      }

      _contextMenu.IsSubmenuOpen = true;

      return metadata;

    }

    private System.Windows.Controls.MenuItem AddMenuItem(string name, string header)
    {
      System.Windows.Controls.MenuItem menuItem = new System.Windows.Controls.MenuItem();
      menuItem.Name = name;
      menuItem.Header = header;
      menuItem.Click += new System.Windows.RoutedEventHandler(menuItem_Click);

      return menuItem;
    }

    void menuItem_Click(object sender, System.Windows.RoutedEventArgs e)
    {
      string header = (sender as System.Windows.Controls.MenuItem).Header.ToString();
      object convertedValue = null;
      if (this.Converter != null)
      {
        convertedValue = this.Converter.CommitedText(new List<ListItem>{new ListItem{Name = header, Value = header}});
      }
      base.OnUpdated(convertedValue);
    }
  }

  //Next: Move to Common
  internal class MyWin32Window : IWin32Window
  {
    private IntPtr _hwnd;

    public MyWin32Window(IntPtr handle)
    {
      _hwnd = handle;
    }

    public IntPtr Handle
    {
      get
      {
        return _hwnd;
      }
    }
  }
 }
